home *** CD-ROM | disk | FTP | other *** search
/ SGI O2 Out of Box Experience / SGI O2 Out of Box Experience.iso / SysTour / ROI.java < prev    next >
Text File  |  1996-11-14  |  8KB  |  291 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.net.*;
  4. import java.util.Enumeration;
  5.  
  6. public class ROI extends Applet{
  7.  
  8.     // Button States
  9.     final private byte UNUSED    = -1;
  10.     final private byte IDLE      =  0;
  11.     final private byte HILIGHT   =  1;
  12.     final private byte PRESSED   =  2;
  13.  
  14.     private byte buttonState = IDLE;
  15.  
  16.     // Media Tracker ID's for Various Imags (Dictates Load Order)
  17.     final private int BACKGROUND_ID = 0;
  18.     final private int IMAGE_ID      = 1000;
  19.     final private int HILIGHT_ID    = 2000;
  20.     final private int PRESSED_ID    = 3000;
  21.     final private int HELP_ID       = 4000;
  22.   
  23.     Image[] regionHilightImage;         // Region Hilight Images
  24.     Image[] regionPressedImage;         // Region Pressed Images
  25.     Image backgroundImg;                // Idle Image
  26.     Polygon[] region;    
  27.     Polygon regionHilightLocation;
  28.     Polygon regionPressedLocation;
  29.     URL[] url;
  30.  
  31.     HelpApplet helpApplet = null;
  32.  
  33.     AudioClip audio;
  34.  
  35.     // Image Locations
  36.     Point imgBackgroundLoc = null;
  37.  
  38.     // Miscelaneous
  39.     int imageCount, regionCount;
  40.     int which;
  41.     int time = 0;
  42.     int framesPerSecond;
  43.     int hilightRegion = UNUSED; 
  44.     Image offscreenImg = null;
  45.     Graphics offscreenGraphics = null;
  46.  
  47.     String targetStr = null;
  48.  
  49.   // End Variables Declarations
  50.  
  51.   // Begin Methods
  52.   public void update(Graphics g){
  53.     if (offscreenImg == null)
  54.     offscreenImg = createImage(size().width,size().height);
  55.     if (offscreenGraphics == null)
  56.         offscreenGraphics = offscreenImg.getGraphics();
  57.  
  58.     draw(offscreenGraphics);
  59.     g.drawImage(offscreenImg,0,0,this);
  60.   }
  61.   
  62.   public void paint(Graphics g){
  63.       update(g);
  64.   }
  65.     
  66.   public void draw(Graphics g){
  67.  
  68.     super.paint(g);
  69.  
  70.     if (backgroundImg != null)
  71.     g.drawImage(backgroundImg, imgBackgroundLoc.x, imgBackgroundLoc.y, this);
  72.  
  73.     if (hilightRegion != UNUSED){
  74.     Point p;
  75.  
  76.     switch (buttonState){
  77.       case HILIGHT:
  78.         if (regionHilightImage != null){
  79.         p = new Point(regionHilightLocation.xpoints[hilightRegion],
  80.                   regionHilightLocation.ypoints[hilightRegion]);
  81.         g.drawImage(regionHilightImage[hilightRegion],p.x,p.y,this);
  82.         }
  83.         break;
  84.       case PRESSED:
  85.         if (regionPressedImage != null){
  86.         p = new Point(regionPressedLocation.xpoints[hilightRegion],
  87.                   regionPressedLocation.ypoints[hilightRegion]);
  88.         g.drawImage(regionPressedImage[hilightRegion],p.x,p.y,this);
  89.         }
  90.         break;
  91.     }
  92.     }
  93.   }
  94.  
  95.   public void init(){
  96.  
  97.     MediaTracker tracker = new MediaTracker(this);
  98.  
  99.     // Load Background Image Parameters
  100.     //
  101.     //   The background image will appear at all times and may
  102.     //   have a blue screen which allows each of the movie images
  103.     //   to be placed within the image.
  104.     //
  105.     //   <PARAM NAME="BACKGROUND_IMAGE" VALUE="myimage.gif">
  106.     //   <PARAM NAME="BACKGROUND_IMAGE_LOCATION" VALUE="(x, y)">
  107.     //
  108.     String backgroundImgStr = getParameter("BACKGROUND_IMAGE");
  109.     String backgroundLocationStr = getParameter("BACKGROUND_IMAGE_LOCATION");
  110.     
  111.     if (backgroundImgStr != null){
  112.     backgroundImg = getImage(getDocumentBase(), backgroundImgStr);
  113.  
  114.         tracker.addImage(backgroundImg,BACKGROUND_ID);
  115.     }
  116.     
  117.     if (backgroundLocationStr != null)
  118.     imgBackgroundLoc =
  119.         StringConversion.string2Point(backgroundLocationStr);
  120.     else
  121.     imgBackgroundLoc = new Point(0,0);
  122.  
  123.     // Load Region Parameters
  124.     //
  125.     //   Regions are rectangular regions of interest whenever the
  126.     //   cursor enters one of these regions, the corressponding
  127.     //   region help image will be loaded into the display box.
  128.     //
  129.     //   <PARAM NAME="TARGET" VALUE="_contents">
  130.     //   <PARAM NAME="REGION_COUNT" VALUE="1">
  131.     //   <PARAM NAME="REGION_HILIGHT_IMAGE_BASENAME" VALUE="HILIGHT">
  132.     //   <PARAM NAME="REGION_PRESSED_IMAGE_BASENAME" VALUE="pressed">
  133.     //   <PARAM NAME="REGION_HILIGHT_IMAGE_LOCATIONS" VALUE="(x, y)">
  134.     //   <PARAM NAME="REGION_PRESSED_IMAGE_LOCATIONS" VALUE="(x, y)">
  135.     //
  136.     //   <PARAM NAME="REGION_0" VALUE="(20, 20), (30, 30)">
  137.     //   <PARAM NAME="REGION_0_URL" VALUE="http://www.sgi.com">
  138.     //
  139.     String regionCountStr = getParameter("REGION_COUNT");
  140.     String regionHilightBasename = getParameter("REGION_HILIGHT_IMAGE_BASENAME");
  141.     String regionPressedBasename = getParameter("REGION_PRESSED_IMAGE_BASENAME");
  142.     String regionHilightLocationStr = getParameter("REGION_HILIGHT_IMAGE_LOCATIONS");
  143.     String regionPressedLocationStr = getParameter("REGION_PRESSED_IMAGE_LOCATIONS");
  144.     targetStr = getParameter("TARGET");
  145.  
  146.     if (targetStr == null)
  147.     targetStr = "_self";
  148.     
  149.     if (regionCountStr != null){
  150.     regionCount = Integer.parseInt(regionCountStr);
  151.     region = new Polygon[regionCount];
  152.     url = new URL[regionCount];
  153.     }
  154.  
  155.     if (regionHilightBasename != null)
  156.     regionHilightImage = new Image[regionCount];
  157.  
  158.     if (regionPressedBasename != null)
  159.     regionPressedImage = new Image[regionCount];
  160.     
  161.     for(int i=0;i<regionCount;i++){
  162.     String regionPolygonStr = getParameter("REGION_" + Integer.toString(i));
  163.     String regionURLStr = getParameter("REGION_" + Integer.toString(i) + "_URL");
  164.     
  165.     if (regionPolygonStr != null)
  166.         region[i] =    StringConversion.string2Polygon(regionPolygonStr);
  167.  
  168.     if (regionHilightLocationStr != null)
  169.         regionHilightLocation = StringConversion.string2Polygon(regionHilightLocationStr);
  170.  
  171.     if (regionPressedLocationStr != null)
  172.         regionPressedLocation = StringConversion.string2Polygon(regionPressedLocationStr);
  173.  
  174.     if (regionURLStr != null)
  175.         try{
  176.         url[i] = new URL(getDocumentBase(),regionURLStr);
  177.         } catch (MalformedURLException e){}
  178.     else
  179.         url[i] = getDocumentBase();
  180.     
  181.     if (regionHilightBasename != null){
  182.         regionHilightImage[i] = getImage(getDocumentBase(),StringConversion.baseString(regionHilightBasename) + 
  183.                          Integer.toString(i) + StringConversion.extString(regionHilightBasename));
  184.         tracker.addImage(regionHilightImage[i],HILIGHT_ID + i);
  185.     }
  186.  
  187.     if (regionPressedBasename != null){
  188.         regionPressedImage[i] = getImage(getDocumentBase(),StringConversion.baseString(regionPressedBasename) + 
  189.                                              Integer.toString(i) + StringConversion.extString(regionPressedBasename));
  190.         tracker.addImage(regionPressedImage[i],PRESSED_ID + i);
  191.     }
  192.  
  193.     }
  194.     
  195.     // Load Audio Parameters
  196.     //
  197.     //   The audio file will be loaded initially and played once
  198.     //   all of the images in the animation have been loaded.
  199.     //
  200.     //   <PARAM NAME="AUDIO_FILE" VALUE="myaudio.au">
  201.     //
  202.     String audioFile = getParameter("AUDIO_FILE");
  203.     String playOnceStr = getParameter("PLAY_ONCE");
  204.     
  205.     if (audioFile != null)
  206.     audio = getAudioClip(getDocumentBase(),audioFile);
  207.  
  208.     // Load all images:
  209.     try{
  210.     tracker.waitForAll();
  211.     } catch (InterruptedException e) {
  212.     return;
  213.     }
  214.     tracker = null;
  215.  
  216.     audio.play();
  217.   }
  218.     
  219.   public boolean mouseDown(Event evt, int x, int y){
  220.   
  221.     if ((hilightRegion = locateRegion(x,y)) != -1){
  222.     buttonState = PRESSED;
  223.     repaint();
  224.     }
  225.  
  226.     return true;
  227.   }
  228.     
  229.   public boolean mouseUp(Event evt, int x, int y){
  230.     buttonState = IDLE;
  231.     int mouseDownRegion = hilightRegion;
  232.  
  233.     if ((hilightRegion = locateRegion(x,y)) != -1 &&
  234.         hilightRegion == mouseDownRegion)
  235.         getAppletContext().showDocument(url[hilightRegion],targetStr);
  236.  
  237.     hilightRegion = -1;
  238.     repaint();
  239.  
  240.     return true;
  241.   }
  242.  
  243.   private int locateRegion(int x, int y){
  244.     for(int i=0;i<regionCount;i++){
  245.     if (region[i].inside(x,y)){
  246.         return i;
  247.     }
  248.     }
  249.  
  250.     return -1;
  251.   }
  252.  
  253.   public boolean mouseMove(Event evt, int x, int y){
  254.  
  255.     if ((hilightRegion = locateRegion(x,y)) != -1){
  256.     buttonState = HILIGHT;
  257.  
  258.     if (helpApplet != null)
  259.         helpApplet.showHelp(hilightRegion);
  260.  
  261.     repaint();
  262.     }
  263.  
  264.     return true;
  265.   }
  266.  
  267.   public boolean mouseExit(Event evt, int x, int y){
  268.     buttonState = IDLE;
  269.     hilightRegion = -1;
  270.  
  271.     if (helpApplet != null)
  272.     helpApplet.showHelp(hilightRegion);
  273.  
  274.     repaint();
  275.     return true;
  276.   }
  277.  
  278. public void start(){
  279.  
  280.     // Had to perform this hack becase getApplet("HelpApplet") would not work
  281.     // w/ Netscape 2.02S...
  282.     for (Enumeration e = getAppletContext().getApplets() ;
  283.      e.hasMoreElements() ;){
  284.     Object o = e.nextElement();
  285.     if (o.getClass().getName().compareTo("HelpApplet") == 0)    
  286.         helpApplet = (HelpApplet) o;
  287.     }
  288. }
  289.  
  290. }
  291.